const express = require("express");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const jwt = require("jsonwebtoken");
const app = express();
app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(cookieParser());
const secret = "abc";
app.post("/login", (req, res) => {
    const user = req.body;
    jwt.sign(user, secret, (err, token) => {
        console.log(token);
        res.cookie("token", token, {
            maxAge: 1000 * 60 * 60
        });
        res.send("登录成功");
    });
});
app.get("/home", (req, res) => {
    console.log(req.cookies);
    jwt.verify(req.cookies.token, secret, (err, authData) => {
        if (err) {
            res.send('重新登录！！');
        } else {
            console.log(authData);
            res.send('home');
        }
    })

});
app.listen(3000, err => {
    if (!err) console.log("启动");
    else console.log(err);
});